package cool.tianyou.blue.bird.common.mapper;

import cool.tianyou.blue.bird.common.model.po.Device;
import java.util.List;
import java.util.Optional;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

/**
 * @author Liutianyou
 * @date 2021/7/18 3:38 下午
 */
@org.apache.ibatis.annotations.Mapper
public interface DeviceMapper extends Mapper<Device> {

  /**
   * 根据设备id 和用户id 查找设备
   *
   * @param deviceId 设备id
   * @param userId   用户id
   * @return 设备
   */
  default Optional<Device> findByIdAndUserId(Integer deviceId, Integer userId) {
    final List<Device> devices = this.selectByExample(
        Example.builder(Device.class).where(Sqls.custom()
            .andEqualTo("id", deviceId)
            .andEqualTo("userId", userId)
        ).build());
    return devices.isEmpty() ? Optional.empty() : Optional.of(devices.get(0));
  }

  default Integer count() {
    return this.selectCountByExample(null);
  }

}
